package model

import (
	"context"

	"gorm.io/gorm"
)

type BlogInfo struct {
	gorm.Model
	UserId     int    `gorm:"not null"`
	Title      string `gorm:"not null"`
	Content    string `gorm:"not null"`
	Category   string `gorm:"default:''"`
	LikeNum    int    `gorm:"default:0"`
	CommentNum int    `gorm:"default:0"`
	Place      string `gorm:"default:''"`
	JoyNum     int    `gorm:"default:0"`
	JoyFeeling string `gorm:"default:''"`
	ReadNum    int    `gorm:"default:0"`
}

func (m *defaultBlogModel) Create(ctx context.Context, blog *BlogInfo) (uint, error) {
	err := m.db.WithContext(ctx).Create(blog).Error
	return blog.ID, err
}

func (m *defaultBlogModel) CreateImages(ctx context.Context, blogId uint, urls []string) error {
	var images []BlogImg
	for _, url := range urls {
		images = append(images, BlogImg{
			BlogId: int(blogId),
			Url:    url,
		})
	}
	return m.db.WithContext(ctx).Create(&images).Error
}

func (m *defaultBlogModel) FindOne(ctx context.Context, id uint) (*BlogInfo, error) {
	var blog BlogInfo
	err := m.db.WithContext(ctx).Where("id = ?", id).First(&blog).Error
	return &blog, err
}

func (m *defaultBlogModel) FindImages(ctx context.Context, blogId uint) ([]string, error) {
	var images []BlogImg
	err := m.db.WithContext(ctx).Where("blog_id = ?", blogId).Find(&images).Error
	if err != nil {
		return nil, err
	}

	var urls []string
	for _, img := range images {
		urls = append(urls, img.Url)
	}
	return urls, nil
}

// 在 defaultBlogModel 中实现新方法
func (m *defaultBlogModel) FindByUserID(ctx context.Context, userId int) ([]*BlogInfo, error) {
	var blogs []*BlogInfo
	err := m.db.WithContext(ctx).
		Where("user_id = ?", userId).
		Order("created_at desc").
		Find(&blogs).Error
	return blogs, err
}

func (m *defaultBlogModel) FindWithImages(ctx context.Context, id uint) (*BlogInfo, []string, error) {
	// 获取博客信息
	blog, err := m.FindOne(ctx, id)
	if err != nil {
		return nil, nil, err
	}

	// 获取图片
	images, err := m.FindImages(ctx, id)
	if err != nil {
		return nil, nil, err
	}

	return blog, images, nil
}

// 在 model/blog.go 中
func (m *defaultBlogModel) IncrementReadCount(ctx context.Context, id uint, count int) error {
	return m.db.WithContext(ctx).
		Model(&BlogInfo{}).
		Where("id = ?", id).
		Update("read_num", gorm.Expr("read_num + ?", count)).
		Error
}

// model/blog.go

func (m *defaultBlogModel) FindByUserIDWithPage(ctx context.Context, userId int, page, size int) ([]*BlogInfo, int64, error) {
	var blogs []*BlogInfo
	var total int64

	// 获取总数
	err := m.db.WithContext(ctx).Model(&BlogInfo{}).
		Where("user_id = ?", userId).
		Count(&total).Error
	if err != nil {
		return nil, 0, err
	}

	// 获取分页数据
	offset := (page - 1) * size
	err = m.db.WithContext(ctx).
		Where("user_id = ?", userId).
		Order("created_at desc").
		Offset(offset).
		Limit(size).
		Find(&blogs).Error

	return blogs, total, err
}

// 在 model/blog.go
func (m *defaultBlogModel) FindWithPage(ctx context.Context, page, size int) ([]*BlogInfo, int64, error) {
	var blogs []*BlogInfo
	var total int64

	// 获取总数
	err := m.db.WithContext(ctx).Model(&BlogInfo{}).Count(&total).Error
	if err != nil {
		return nil, 0, err
	}

	// 获取分页数据
	offset := (page - 1) * size
	err = m.db.WithContext(ctx).
		Order("created_at desc").
		Offset(offset).
		Limit(size).
		Find(&blogs).Error

	return blogs, total, err
}

func (m *defaultBlogModel) CheckAuthor(ctx context.Context, id uint, userId int) (bool, error) {
	var blog BlogInfo
	err := m.db.WithContext(ctx).Where("id = ?", id).First(&blog).Error
	if err != nil {
		return false, err
	}
	return blog.UserId == userId, nil
}
